﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace TextMIS
{
    public partial class frmItem : Form
    {
        public frmItem()
        {
            InitializeComponent();
        }

        public static string userName;
        public static string strSql;
        SqlConnection conn;
        SqlCommand com;
        SqlDataAdapter da;
 //       SqlDataReader dr;

        private void frmItem_Load(object sender, EventArgs e)
        {
            userName = frmLogin.userName;
            conn = new
SqlConnection(frmLogin.SQLstr);
            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            listView1.View = View.Details;
            listView1.Scrollable = true;
            listView1.MultiSelect = false;
            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;

            this.ShowItem();
            this.InitComBoBox();

            txtoldNo.Enabled = false;
            txtoldName.Enabled = false;
            txtoldPrice.Enabled = false;
            cbxoldBranch.Enabled = false;
            txtoldMemory.Enabled = false;

        }

        private void InitComBoBox()
        {
            da = new SqlDataAdapter("select BranchNo,BranchName from BranchInfo ", conn);
            DataSet ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "BranchInfo");

            conn.Close();

            this.cbxBranchName.DataSource = ds.Tables[0];
            this.cbxBranchName.DisplayMember = "BranchName";
            this.cbxBranchName.ValueMember = "BranchNo";

            this.cbxoldBranch.DataSource = ds.Tables[0];
            this.cbxoldBranch.DisplayMember = "BranchName";
            this.cbxoldBranch.ValueMember = "BranchNo";
        }

        public void ClearUnblock()
        {
            txtoldNo.Text = listView1.SelectedItems[0].SubItems[0].Text;
            txtoldName.Text = listView1.SelectedItems[0].SubItems[1].Text;
            cbxoldBranch.Text = listView1.SelectedItems[0].SubItems[2].Text;
            txtoldPrice.Text = listView1.SelectedItems[0].SubItems[3].Text;
            txtoldMemory.Text = listView1.SelectedItems[0].SubItems[4].Text;
            txtoldNo.Enabled = true;
            txtoldName.Enabled = true;
            txtoldPrice.Enabled = true;
            cbxoldBranch.Enabled = true;
            txtoldMemory.Enabled = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            com = new SqlCommand("insert into ItemInfo(ItemNo,ItemName,BranchName,ItemPrice,ItemDetails)values('" + txtItemNo.Text.Trim() +
"','" + txtItemName.Text.Trim() + "','" + cbxBranchName.Text.Trim() + "','" + txtPrice.Text.Trim() + "','" + txtMemory.Text.Trim() + "')", conn);

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            com.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("恭喜你！添加成功！");
            this.ShowItem();
        }

        private void SetupHeaderText()
        {
            listView1.Columns.Add("项目编号", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("项目名称", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("所属科室", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("项目价格", 120, HorizontalAlignment.Left);
            listView1.Columns.Add("备注", 200, HorizontalAlignment.Left);
        }

        private void ShowItem()
        {
            try
            {
                listView1.Clear();
                da = new SqlDataAdapter("select ItemNo, ItemName, BranchName, ItemPrice,ItemDetails from ItemInfo", conn);

                DataTable dt = new DataTable();
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                int count = da.Fill(dt);
                conn.Close();

                SetupHeaderText();
                foreach (DataRow dr in dt.Rows)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Clear();
                    lvi.SubItems[0].Text = dr[0].ToString();
                    lvi.SubItems.Add(dr[1].ToString());
                    lvi.SubItems.Add(dr[2].ToString());
                    lvi.SubItems.Add(dr[3].ToString());
                    lvi.SubItems.Add(dr[4].ToString());
                    listView1.Items.Add(lvi);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ClearUnblock();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            com = new SqlCommand("update ItemInfo set ItemNo = '" +
       txtoldNo.Text.Trim() + "',ItemName = '" + txtoldName.Text.Trim() + "',BranchName = '" +
       cbxoldBranch.Text.Trim() + "',ItemPrice = '" + txtoldPrice.Text.Trim() + "',ItemDetails = '" + txtoldMemory.Text.Trim() + "'where ItemNo='" + txtoldNo.Text.Trim() + "'", conn);

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            com.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("修改成功！");
            this.ShowItem();
        }

        private void txtDele_Click(object sender, EventArgs e)
        {
            com = new SqlCommand("delete ItemInfo where ItemNo = '" + listView1.SelectedItems[0].SubItems[0].Text + "'", conn);

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            com.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("修改成功！");
            this.ShowItem();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
